#ifndef HMIELEMENT_H
#define HMIELEMENT_H

#include <QGraphicsObject>
#include <QColor>
#include <QPushButton>
#include <QLabel>
// 前向声明，头文件中参数用到了指针
class QPainter;
class QGraphicsSceneMouseEvent;
class QGraphicsSceneContextMenuEvent;
class QGraphicsSceneHoverEvent;

enum ComponentType
{
    BUTTON,
    INDICATOR
};

// HMI组件基类
class HmiComponent : public QGraphicsObject
{
    Q_OBJECT
public:
    explicit HmiComponent(QGraphicsItem *parent = nullptr);

    void setColor(const QColor& color); // 当前显示颜色
    QColor color() const;
    void setComponentName(const QString& name);
    QString componentName() const;
    // 设置 ON / OFF 状态颜色
    void setOnColor(const QColor& color);
    QColor onColor() const;
    void setOffColor(const QColor& color);
    QColor offColor() const;
    void setAddress(int address);
    int address() const;
    void setSize(double width, double height);
    QSizeF size() const;

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
    QRectF boundingRect() const override;

Q_SIGNALS:
    void selected(HmiComponent* item);
    void copyRequested(HmiComponent* item);
    void deleteRequested(HmiComponent* item);

    // 用于改变状态颜色
    void changeOnColorRequested(HmiComponent* item);
    void changeOffColorRequested(HmiComponent* item);

    void coilAddressChanged(int oldAddress, int newAddress);

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
    void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
    virtual void paintShape(QPainter* painter) = 0;
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
    void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override;
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;

private:
    // 显示属性对话框
    void propertyDialog();
    // 创建颜色预览标签（提取重复逻辑）
    QLabel* colorDisplayLabel(const QColor& color);

protected:
    QColor color_;             // 当前显示颜色（编辑器中为 OFF）
    QColor onColor_ = Qt::green; // 供非按钮组件使用
    QColor offColor_;
    QString componentName_;
    int address_ = 0;

    // 尺寸成员
    double width_ = 65.0;
    double height_ = 30.0;

    bool resizing_ = false;
    QPointF resizeStartPos_;
    QSizeF resizeStartSize_;
    // 标签区域高度
    static constexpr double labelHeight = 12.0;
};

// 按钮类
class HmiButton : public HmiComponent
{
    Q_OBJECT
public:
    HmiButton(QGraphicsItem *parent = nullptr);

    // 状态标志，true = ON，false = OFF
    bool isOn() const;
    void setOn(bool on);

Q_SIGNALS:
    void stateChanged(bool on);

protected:
    void paintShape(QPainter *painter) override;
    // 为了按下变深、松开恢复并切换状态
    void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override; // 鼠标移动事件处理

private:
    bool isOn_ = false; // 默认OFF
    bool pressed_ = false; // 用于按压视觉
    bool isClicked_ = false; // 是否是点击事件
};

// 指示灯类
class HmiIndicator : public HmiComponent
{
public:
    HmiIndicator(QGraphicsItem *parent = nullptr);
    bool isOn() const;

protected:
    void paintShape(QPainter *painter) override;
};

#endif // HMIELEMENT_H
